<?php

namespace app\api\controller;

use addons\epay\library\Service;
use app\admin\model\goods\Order;
use app\common\controller\Api;
use app\common\controller\Payment;
use think\Controller;
use think\Db;
use think\Request;


class District extends Api
{
    private static $model;
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';


    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        self::$model = new \app\admin\model\District();
    }

    /**
     * 获取所有的地址
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function lists()
    {
        $order = input('order');
        if ($order) {
            $order .= ' asc';
        } else {
            $order = 'id asc';
        }
        $areas = Db('district')->field('id,cid,name,pid')->order($order)->select();
        $data = get_tree_list($areas);
        $this->success('所有地址', $data);
    }

    /**
     * 获取某级所有地址
     */
    public function listsByLevel()
    {
        $level = input('level', 1);
        $order = input('order');
        if ($order) {
            $order .= ' asc';
        } else {
            $order = 'id asc';
        }
        $areas = Db('area')->where('level', $level)->order($order)->select();
        $this->success('某级所有地址', $areas);
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function getListsByPid()
    {
        $pid = input('pid', 0);
        $where = [
            'pid' => $pid
        ];
        $order = input('order', 'id');
        $sort = input('sort', 'asc');

        $list = self::$model->where($where)->order($order . ' ' . $sort)->select();
        $this->success('下级区域列表', $list);
    }
}
